<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>promise</title>
</head>
<body>

</body>
<script>
    // 省 --> 市 --> 县
   /* $.ajax({   //回调地狱
        url: "A",
        success: function () {
            $.ajax({
                url: "B",
                success: function () {
                    $.ajax({
                        url: "C",
                        success: function () {

                        }
                    })
                }
            })
        },
        error:function () {

        }
    })*/
    //回调地狱
    //代码可读性很差
    //复杂度提高

//   promise   承诺  表示未来某个时间一定会有答复.


    //Promise //类  异步解决方案
    //promise 中有三种状态
    //pending(进行中) fulfilled(已成功) rejected(已失败)
    //pending(进行中)  ===>  fulfilled(已成功)
    //pending(进行中)  ===>  rejected(已失败)


    {
       /* //一旦Promise创建成功会立即执行
        let p1 = new Promise((resolve, reject)=>{
            //resolve(成功时的回调)
            //reject(失败时的回调)
            resolve();
            console.log(1);
//            reject();
        });
//        p1.then(成功时的回调[,失败时的回调]);
        p1.then(
            success=>{
                console.log(2)
            }
        );

        console.log(3)
        //输出: 1 3 2*/

    }
    {
        let p1 = new Promise((resolve, reject) => {
            //resolve(成功时的回调)
            //reject(失败时的回调)
            reject();
            resolve();
            console.log(1);
        });
        p1.then(
            success => {
                console.log("success---1")
            },
            err=>{
                console.log("err---1")
            }
        );
        p1.then(
            success=>{
                console.log("success---2")
            },
            err=>{
                console.log("err---2")
            }
        )
    }
    {
        // 省 --> 市 --> 县
 /*       let p1 = ()=>{
            return new Promise((resolve, reject)=>{
                setTimeout(()=>{ //$.ajax
                    console.log("省");
                    resolve();
                },3000)
            })
        };

        let p2 = p1().then(
            success=>{
                return new Promise((resolve, reject)=>{
                    setTimeout(()=>{
                        console.log("市");
                        resolve();
                    },2000)

                })
            }
        );
        p2.then(success=>{
            console.log("县");
        })*/
    }
    {
        //catch() 捕获 发生错误时的回调函数, 返回promise对象
        //finally()

       /*
        try{
            throw new Error("模拟代码报错")
        }catch (err){

        }finally{

        }*/



        let pro1 = (num)=>{
            return new Promise((resolve, reject)=>{
                if(num>0){
                    resolve("大于0的数字")
                }else {
                    throw new Error("模拟代码报错")
                }
            })
        };

        pro1(-1).then(
            success=>{
                console.log("success===>",success);
//                throw new Error("模拟代码报错2222");
            },
//            err=>{
//                console.log("err===>", err)
//            }
        ).catch(
            catchErr=>{
                console.log("err==catch=>",catchErr)
            }
        ).finally(
            final=>{
                console.log("final==final=>")
            }
        )



    }

    /*
     异步解决方案
    * Promise对象代表一个异步操作, 它表示一个在未来完成的异步操作
    * 有三种状态: pending(进行中)\fulfilled(已成功)\rejected(已失败)
    * 状态改变只有两种情况: pending==>fulfilled  || pending==>rejected
    * 一旦状态发生改变, 就会一直保持这个状态,不再发生变化
    * 一旦状态改变, promise.then方法就会执行
    * promise有两个参数resolve || reject 是两个回调函数
    * all()和race()
    * */

    {
        let p1 = Promise.resolve(1);
        //p1等价于p2
        let p2 = new Promise((resolve,reject)=>{
            resolve(1)
        });


        p1.then(
            success=>{
                console.log("Promise.resolve(1) --- success----",success);
            }
        );
        p2.then(
            success=>{
                console.log("Promise.resolve(1) --- success----",success);
            }
        )

    }
    {
        let p1 = Promise.reject(1);
        //p1等价于p2
        let p2 = new Promise((resolve,reject)=>{
            reject(1)
        });


        p1.then(
            success=>{
                console.log("Promise.reject --- success----",success);
            },
            err=>{
                console.log("Promise.reject --- err----",err);
            }
        );
        p2.then(
            success=>{
                console.log("Promise.reject --- success----",success);
            },
            err=>{
                console.log("Promise.reject --- err----",err);
            }
        )

    }
    {//promise-all()和race();
//        let pAll = Promise.all([p1,p2,p3])
//        let pRace = Promise.race([p1,p2,p3])

        let p1 = new Promise((resolve, reject) => {
            setTimeout(resolve, 2000, 'p1');
        });
        let p2 = new Promise((resolve, reject) => {
            setTimeout(resolve, 3000, 'p2')
        });
        let p3 = new Promise((resolve, reject) => {
            setTimeout(reject, 1000, 'p3')
        });

        let pAll = Promise.all([p1,p2,p3]);
        pAll.then(
            success=>{
                console.log(success);
            },
            err=>{
                console.log("err===>",err);
            }
        )
    }
    {//promise-all()和race();
//        let pAll = Promise.all([p1,p2,p3])
//        let pRace = Promise.race([p1,p2,p3])

        let p1 = new Promise((resolve, reject) => {
            setTimeout(reject, 2000, 'p1');
        });
        let p2 = new Promise((resolve, reject) => {
            setTimeout(resolve, 3000, 'p2')
        });
        let p3 = new Promise((resolve, reject) => {
            setTimeout(resolve, 1000, 'p3')
        });

        let pAll = Promise.race([p1,p2,p3]);
        pAll.then(
            success=>{
                console.log("race==success=>",success);
            },
            err=>{
                console.log("race==err===>",err);
            }
        )
    }

    {
        //
        let pAll = Promise.race([p1,p2]);
    }




















</script>
</html>